package weak_20241227_29.io;

import java.io.*;

/**
 * @author zdl
 * @date 2024/12/28
 */
public class Demo2 {
    public static void main(String[] args) throws IOException {
        //2. 编写一个Java程序，将用户桌面上的"example.txt"文件复制到当前目录下的"copy_example.txt"。
       /* FileInputStream fis = new FileInputStream("D:/test/example.txt");
        FileOutputStream fos = new FileOutputStream("D:/test/copy_example.txt");
        byte[] bty = new byte[8];
        int len;
        while ((len = fis.read(bty)) != -1){
            fos.write(bty,0,len);
        }
        fis.close();
        fos.close();*/
        copyTxt();
    }
    //使用默认缓冲写法

    /**
     * 关闭流 一定是关闭bos 后关闭bis
     * @throws IOException
     */
    public static void copyTxt() throws IOException {
        FileInputStream fis  = new FileInputStream("D:/test/example.txt");
        BufferedInputStream bis = new BufferedInputStream(fis);
        FileOutputStream fos  = new FileOutputStream("D:/test/copy_example.txt");
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        int length;
        while ((length = bis.read()) != -1){
            bos.write(length);
        }
        bos.close();
        bis.close();
    }
}
